﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ScannerDemo.common;
using Microsoft.Phone.Tasks;
using ContactChooser;
using System.Windows.Input;


namespace ScannerDemo
{
    public partial class GenerateVcard : PhoneApplicationPage
    {
        ApplicationBarIconButton appBarIconBtnQR = new ApplicationBarIconButton(new Uri("/icons/confirm.png", UriKind.Relative));
        ApplicationBarIconButton appBarIconBtnRelocation = new ApplicationBarIconButton(new Uri("/icons/scan icon.png", UriKind.Relative));
        DataValidator dataValidator = new DataValidator();
        public GenerateVcard()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                #region C#生成ApplicationBar已经相关的Button和事件.
                ApplicationBar = new ApplicationBar();

                //ApplicationBarIconButton appBarIconBtnQR = new ApplicationBarIconButton(new Uri("/icons/appbar.refresh.rest.png", UriKind.Relative));
                appBarIconBtnQR.Text = "生成";
                appBarIconBtnQR.Click += new EventHandler(ApplicationBar_Generation_Click);
                appBarIconBtnQR.IsEnabled = false;
                ApplicationBar.Buttons.Add(appBarIconBtnQR);


                
                appBarIconBtnRelocation.Text = "扫描";
                appBarIconBtnRelocation.Click += new EventHandler(ApplicationBar_StartPage_Click);
                ApplicationBar.Buttons.Add(appBarIconBtnRelocation);

                //ApplicationBar.IsVisible = true;
                checkAllData();

                #endregion

            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            //checkAllData();
            base.OnNavigatedTo(e);

        }
        private void ApplicationBar_Generation_Click(object sender, EventArgs e)
        {
            Vcard vcard = new Vcard();
            vcard.name = textInputName.Text;
            vcard.tel = textInputPhone.Text;
            vcard.email = textInputEmail.Text;
            vcard.company = textInputCompany.Text;
            vcard.occupation = textInputOccupation.Text;

            string uriString = "" + vcard.generateString();
            App.PublicText = uriString;
            NavigationService.Navigate(new Uri("/GenerateResult.xaml", UriKind.RelativeOrAbsolute));
        }


        private void ApplicationBar_StartPage_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            //appBarIconBtnQR.IsEnabled = false;
            checkAllData();
        }


        private void textInputName_LostFocus(object sender, RoutedEventArgs e)
        {
            
            if (textInputName.Text != "")
                checkAllData();
            else
            {
                appBarIconBtnQR.IsEnabled = false;
                //MessageBox.Show("请输入姓名!");
            }
        }




        private void textInputEmail_LostFocus(object sender, RoutedEventArgs e)
        {
            
            if (dataValidator.IsValidEmail(textInputEmail.Text))
                checkAllData();
            else
            {
                appBarIconBtnQR.IsEnabled = false;
                MessageBox.Show("请输入正确的Email!");
            }
        }
        private void checkAllData()
        {
            if((dataValidator.IsValidName(textInputName.Text)&&(dataValidator.IsValidEmail(textInputEmail.Text))))
                appBarIconBtnQR.IsEnabled=true;
            else
                appBarIconBtnQR.IsEnabled=false;


        }


        private void textInputName_TextChanged(object sender, TextChangedEventArgs e)
        {
            checkAllData();
        }

        private void Button_Click_PhoneBook(object sender, RoutedEventArgs e)
        {
            //调用选择器选择联系人.
            var contactChooser = new ContactChooserTask();
            contactChooser.OnSelected += new EventHandler<ContactChooserEventArgs>(contactChooser_OnSelected);
            contactChooser.Show();

            /*
            PhoneNumberChooserTask phoneNumberChooserTask = new PhoneNumberChooserTask();
            phoneNumberChooserTask.Show();
            phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
             */
        }
        void contactChooser_OnSelected(object sender, ContactChooserEventArgs e)
        {
            /*
            if (e.Contact. == null)
            {
                if (e.TaskResult == TaskResult.OK)
                {
             * */
            try
            {
                textInputName.Text = e.Contact.DisplayName;
            }
            catch { }
            try
            {
                textInputPhone.Text = e.Contact.PhoneNumbers.FirstOrDefault().PhoneNumber;
            }
            catch { }
            try
            {
                textInputEmail.Text = e.Contact.EmailAddresses.FirstOrDefault().EmailAddress;
            }
            catch { }
            try
            {
                textInputCompany.Text = e.Contact.Companies.FirstOrDefault().CompanyName;
            }
            catch { }
            try
            {
                textInputOccupation.Text = e.Contact.Companies.FirstOrDefault().JobTitle;
            }
            catch { }
            
                   
            /*
                }
                else
                    MessageBox.Show("您取消了当前选择联系人的任务!", "系统提示", MessageBoxButton.OK);
            }
            else
            {
                MessageBox.Show("选择联系错误!", "系统提示", MessageBoxButton.OK);
                //this.pivotMap.SelectedIndex = 1;
            }
             
            MessageBox.Show(e.Contact.DisplayName);
             */
        }

        private void textInputName_KeyDown_1(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.PlatformKeyCode == 0x0A)
                textInputPhone.Focus(); 
        }

        private void textInputPhone_KeyDown_1(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.PlatformKeyCode == 0x0A)
                textInputEmail.Focus(); 
        }

        private void textInputEmail_KeyDown_1(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.PlatformKeyCode == 0x0A)
                textInputCompany.Focus(); 
        }

        private void textInputCompany_KeyDown_1(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.PlatformKeyCode == 0x0A)
                textInputOccupation.Focus(); 
        }

        private void textInputOccupation_KeyDown_1(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        private void textInputEmail_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            checkAllData();
        }
        //选择联系人的完成事件
        /*
        void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
        {
            if (e.Error == null)
            {
                if (e.TaskResult == TaskResult.OK)
                {
                    textInputName.Text = e.DisplayName;
                    textInputPhone.Text = e.PhoneNumber;
                   
                }
                else
                    MessageBox.Show("您取消了当前选择联系人的任务!", "系统提示", MessageBoxButton.OK);
            }
            else
            {
                MessageBox.Show("选择联系错误!", "系统提示", MessageBoxButton.OK);
                //this.pivotMap.SelectedIndex = 1;
            }
        }
         * */
    }
}